package com.icepeach.icepeachhomeback.exception;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.icepeach.icepeachhomeback.util.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class MyExceptionHandler {

    /**
     * 如果抛出的是ServiceException,则调用该方法
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ApiResult handle(ServiceException serviceException) {
        if(ObjUtil.isEmpty(serviceException.getCode())){
            return ApiResult.normal(ApiResult.CODE_SERVER_ERROR, serviceException.getMessage(), null);
        }else {
            return ApiResult.normal(serviceException.getCode(), serviceException.getMessage(), null);

        }
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult handleException(Exception exception) {
        log.info("",exception);
        return ApiResult.failure(ApiResult.CODE_SERVER_ERROR,"服务开小差啦~,请联系管理员");
    }
}
